package com.singletonapps.evory.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.singletonapps.evory.dao.CourseDao;
import com.singletonapps.evory.entities.Course;

@Repository
public class CourseDaoImpl implements CourseDao {

	@Autowired
	private SessionFactory session;
	
	
	@Override
	public Long create(Course curso) {
		Long id = (Long) session.getCurrentSession().save(curso);
		session.getCurrentSession().flush();
		return id;
	}


	@Override
	public Course findOneById(Long id) {
		
        Query query = session.getCurrentSession().createQuery("FROM Course c where c.id = :id");
        query.setParameter("id", id);
        
        return (Course) query.uniqueResult();
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<Course> findAll() {
		Query query = session.getCurrentSession().createQuery("FROM Course c");
		
		return (List<Course>) query.list();
	}

}
